package ca.liu.scaffoldingmvctest.service;

import ca.liu.scaffoldingmvctest.dao.StLogDAO;
import ca.liu.scaffoldingmvctest.dao.StUserDAO;

import ca.liu.scaffoldingmvctest.domain.StLog;
import ca.liu.scaffoldingmvctest.domain.StUser;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for StLog entities
 * 
 */

@Service("StLogService")
@Transactional
public class StLogServiceImpl implements StLogService {

	/**
	 * DAO injected by Spring that manages StLog entities
	 * 
	 */
	@Autowired
	private StLogDAO stLogDAO;

	/**
	 * DAO injected by Spring that manages StUser entities
	 * 
	 */
	@Autowired
	private StUserDAO stUserDAO;

	/**
	 * Instantiates a new StLogServiceImpl.
	 *
	 */
	public StLogServiceImpl() {
	}

	/**
	 * Return a count of all StLog entity
	 * 
	 */
	@Transactional
	public Integer countStLogs() {
		return ((Long) stLogDAO.createQuerySingleResult("select count(o) from StLog o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing StUser entity
	 * 
	 */
	@Transactional
	public StLog saveStLogStUser(Integer id, StUser related_stuser) {
		StLog stlog = stLogDAO.findStLogByPrimaryKey(id, -1, -1);
		StUser existingstUser = stUserDAO.findStUserByPrimaryKey(related_stuser.getId());

		// copy into the existing record to preserve existing relationships
		if (existingstUser != null) {
			existingstUser.setId(related_stuser.getId());
			existingstUser.setPassword(related_stuser.getPassword());
			existingstUser.setUsername(related_stuser.getUsername());
			related_stuser = existingstUser;
		} else {
			related_stuser = stUserDAO.store(related_stuser);
			stUserDAO.flush();
		}

		stlog.setStUser(related_stuser);
		related_stuser.getStLogs().add(stlog);
		stlog = stLogDAO.store(stlog);
		stLogDAO.flush();

		related_stuser = stUserDAO.store(related_stuser);
		stUserDAO.flush();

		return stlog;
	}

	/**
	 * Load an existing StLog entity
	 * 
	 */
	@Transactional
	public Set<StLog> loadStLogs() {
		return stLogDAO.findAllStLogs();
	}

	/**
	 * Delete an existing StLog entity
	 * 
	 */
	@Transactional
	public void deleteStLog(StLog stlog) {
		stLogDAO.remove(stlog);
		stLogDAO.flush();
	}

	/**
	 * Return all StLog entity
	 * 
	 */
	@Transactional
	public List<StLog> findAllStLogs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<StLog>(stLogDAO.findAllStLogs(startResult, maxRows));
	}

	/**
	 */
	@Transactional
	public StLog findStLogByPrimaryKey(Integer id) {
		return stLogDAO.findStLogByPrimaryKey(id);
	}

	/**
	 * Save an existing StLog entity
	 * 
	 */
	@Transactional
	public void saveStLog(StLog stlog) {
		StLog existingStLog = stLogDAO.findStLogByPrimaryKey(stlog.getId());

		if (existingStLog != null) {
			if (existingStLog != stlog) {
				existingStLog.setId(stlog.getId());
				existingStLog.setValue(stlog.getValue());
			}
			stlog = stLogDAO.store(existingStLog);
		} else {
			stlog = stLogDAO.store(stlog);
		}
		stLogDAO.flush();
	}

	/**
	 * Delete an existing StUser entity
	 * 
	 */
	@Transactional
	public StLog deleteStLogStUser(Integer stlog_id, Integer related_stuser_id) {
		StLog stlog = stLogDAO.findStLogByPrimaryKey(stlog_id, -1, -1);
		StUser related_stuser = stUserDAO.findStUserByPrimaryKey(related_stuser_id, -1, -1);

		stlog.setStUser(null);
		related_stuser.getStLogs().remove(stlog);
		stlog = stLogDAO.store(stlog);
		stLogDAO.flush();

		related_stuser = stUserDAO.store(related_stuser);
		stUserDAO.flush();

		stUserDAO.remove(related_stuser);
		stUserDAO.flush();

		return stlog;
	}
}
